home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Graphics / SPD / Sources / lattice.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-10  |  4.4 KB  |  159 lines  |  [TEXT/R*ch]

  1. /*
  2.  * lattice.c - Create a set of shiny spheres, with each sphere connected to
  3.  *         its neighbors by matte cylinders, forming a cubic lattice.
  4.  *    Six light sources.
  5.  *
  6.  * Author:  Antonio Costa, INESC-Norte.
  7.  * Version:  3.1 (04/09/93)
  8.  *
  9.  * SizeFactor determines the number of objects output.
  10.  *    Total spheres   = (SF+1)**3.
  11.  *    Total cylinders = 3*(SF**3)+...
  12.  *
  13.  *    SizeFactor    # spheres    # cylinders
  14.  *         1            8             12
  15.  *         2            27             54
  16.  *         3            64            144
  17.  *
  18.  *         5           216            540
  19.  *
  20.  *        12           2197            6084
  21.  */
  22.  
  23. #include <stdio.h>
  24. #include <math.h>
  25. #include <stdlib.h>    /* atoi */
  26. #include "def.h"
  27. #include "drv.h"        /* display_close() */
  28. #include "lib.h"
  29.  
  30. /* These may be read from the command line */
  31. static int size_factor        = 12;
  32. static int raytracer_format = OUTPUT_RT_DEFAULT;
  33. static int output_format    = OUTPUT_CURVES;
  34.  
  35. #ifdef OUTPUT_TO_FILE
  36. static FILE * stdout_file = NULL;
  37. #else
  38. #define stdout_file stdout
  39. #endif /* OUTPUT_TO_FILE */
  40.  
  41. #define RADIUS1 (1.0 / 4.0)
  42. #define RADIUS2 (1.0 / 16.0)
  43.  
  44. #define inv_factor (1.0 / (double) size_factor)
  45. #define radius1 ((double) RADIUS1 / (double) size_factor)
  46. #define radius2 ((double) RADIUS2 / (double) size_factor)
  47.  
  48. main(argc, argv)
  49.     int        argc;
  50.     char    *argv[];
  51. {
  52.     COORD4    back_color, obj_color;
  53.     COORD4    light;
  54.     COORD4    from, at, up;
  55.     COORD4    center, center1, center2;
  56.     long    x, y, z;
  57.     double    delta, x0, y0, z0, lscale;
  58.  
  59.     PLATFORM_INIT(SPD_LATTICE);
  60.  
  61.     /* Start by defining which raytracer we will be using */
  62.     if ( lib_gen_get_opts( argc, argv,
  63.             &size_factor, &raytracer_format, &output_format ) ) {
  64.     return EXIT_FAIL;
  65.     }
  66.     if ( lib_open( raytracer_format, "Lattice.out" ) ) {
  67.     return EXIT_FAIL;
  68.     }
  69.  
  70.     delta =
  71.     (radius1 * (1.0 - sqrt((double) RADIUS2 / (double) RADIUS1))) * 0.99;
  72.  
  73.     /* output background color - UNC sky blue */
  74.     /* NOTE: Do this BEFORE lib_output_viewpoint(), for display_init() */
  75.     SET_COORD3(back_color, 0.078, 0.361, 0.753);
  76.     lib_output_background_color(back_color);
  77.  
  78.     /* output viewpoint */
  79.     SET_COORD3(from, ((double) (size_factor >> 1) + 0.5) * inv_factor, 1.0,
  80.              1.0 - 1.0 / (double) (size_factor << 1));
  81.     SET_COORD3(at, ((double) (size_factor >> 1) + 0.5) * inv_factor, -1.0,
  82.              -1.0 - 1.0 / (double) (size_factor << 1));
  83.     SET_COORD3(up, 0.2, 1.0, 0.0);
  84.     lib_output_viewpoint(from, at, up, 60.0, 1.0, 0.0, 512, 512);
  85.  
  86.     /* output light sources */
  87.     lscale = 1.0 / sqrt(6.0);
  88.  
  89.     SET_COORD4(light, 2.0, 0.5, 0.5, lscale);
  90.     lib_output_light(light);
  91.     SET_COORD4(light, -1.0, 0.5, 0.5, lscale);
  92.     lib_output_light(light);
  93.     SET_COORD4(light, 0.5, 2.0, 0.5, lscale);
  94.     lib_output_light(light);
  95.     SET_COORD4(light, 0.5, -1.0, 0.5, lscale);
  96.     lib_output_light(light);
  97.     SET_COORD4(light, 0.5, 0.5, 2.0, lscale);
  98.     lib_output_light(light);
  99.     SET_COORD4(light, 0.5, 0.5, -1.0, lscale);
  100.     lib_output_light(light);
  101.  
  102.     for (x = 0; x <= size_factor; x++) {
  103.     x0 = (double) x / (double) size_factor;
  104.     for (y = 0; y <= size_factor; y++) {
  105.         y0 = (double) y / (double) size_factor;
  106.         for (z = 0; z <= size_factor; z++) {
  107.  
  108.         PLATFORM_MULTITASK();
  109.  
  110.         z0 = (double) z / (double) size_factor;
  111.  
  112.         SET_COORD3(obj_color, 0.9, 0.9, 0.9);
  113.         lib_output_color(NULL, obj_color,
  114.             0.0, 0.5, 0.5, 50.0, 5.0, 0.0, 0.0);
  115.  
  116.         SET_COORD4(center, x0, y0, z0, radius1);
  117.         lib_output_sphere(center, output_format);
  118.  
  119.         if (x != size_factor) {
  120.             SET_COORD3(obj_color, 0.9, 0.1, 0.1);
  121.             lib_output_color(NULL, obj_color,
  122.                 0.1, 0.99, 0.0, 0.0, 0.0, 0.0, 0.0);
  123.  
  124.             SET_COORD4(center1, x0 + delta, y0, z0, radius2);
  125.             SET_COORD4(center2, x0 + inv_factor - delta, y0, z0,
  126.                 radius2);
  127.             lib_output_cylcone(center1, center2, output_format);
  128.         }
  129.         if (y != size_factor) {
  130.             SET_COORD3(obj_color, 0.1, 0.9, 0.1);
  131.             lib_output_color(NULL, obj_color,
  132.                 0.1, 0.99, 0.0, 0.0, 0.0, 0.0, 0.0);
  133.  
  134.             SET_COORD4(center1, x0, y0 + delta, z0, radius2);
  135.             SET_COORD4(center2, x0, y0 + inv_factor - delta, z0,
  136.                 radius2);
  137.             lib_output_cylcone(center1, center2, output_format);
  138.         }
  139.         if (z != size_factor)
  140.         {
  141.             SET_COORD3(obj_color, 0.1, 0.1, 0.9);
  142.             lib_output_color(NULL, obj_color,
  143.                 0.1, 0.99, 0.0, 0.0, 0.0, 0.0, 0.0);
  144.  
  145.             SET_COORD4(center1, x0, y0, z0 + delta, radius2);
  146.             SET_COORD4(center2, x0, y0, z0 + inv_factor - delta,
  147.                 radius2);
  148.             lib_output_cylcone(center1, center2, output_format);
  149.         }
  150.         }
  151.     }
  152.     }
  153.  
  154.     lib_close();
  155.  
  156.     PLATFORM_SHUTDOWN();
  157.     return EXIT_SUCCESS;
  158. }
  159.